local H = {}

--判断上下家
---@param player SeverPlayer
---@param num integer
---@param ignoreRemoved boolean
H.getRealNext = function(player, num, ignoreRemoved)
  if #Fk:currentRoom().alive_players == 0 then
    return player.rest > 0 and player.next.rest > 0 and player.next or player
  end
  local doNotIgnore = not ignoreRemoved
  if doNotIgnore and table.every(Fk:currentRoom().players, function(p) return p:isRemoved() end) then
    return player
  end

  local ret = player
  num = num or 1
  for _ = 1, num do
    ret = ret.next
    while doNotIgnore and ret:isRemoved() do
      ret = ret.next
    end
  end
  return ret
end

---@param player SeverPlayer
---@param num integer
---@param ignoreRemoved boolean
H.getRealLast = function(player, num, ignoreRemoved)
  num = num or 1
  local index = (ignoreRemoved and #Fk:currentRoom().players or #table.filter(Fk:currentRoom().players, function(p) return not p:isRemoved() end)) - num
  return HM.getRealNext(player, index, ignoreRemoved)
end

--判断是否为孤军（没有与其相同势力的其他角色）
---@param player SeverPlayer
H.isAlone = function(player)
  if table.every(player.room:getOtherPlayers(player), function(p)
    return not H.compareKingdomWith(p, player)
  end) then
    return true
  end
  return false
end

return H
